//
//  LRStatusCell.swift
//  LRWeibo
//
//  Created by admin on 15/9/26.
//  Copyright © 2015年 懒人. All rights reserved.
//

import UIKit

/// 头像大小
let LRStatusCellIconWidth: CGFloat = 35
/// 控件间距
let LRStatusCellMargin: CGFloat = 12

class LRStatusCell: UITableViewCell {

     // MARK: - 模型数据
    var statusModel: LRStatusListModel?{
    
        didSet{
            // 赋值各模型
            topView.viewModel = statusModel
            contentLabel.text = statusModel?.text
            pictureView.viewModel = statusModel
            pictureView.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(pictureView.bounds.width)
                make.height.equalTo(pictureView.bounds.height)
                
                let offset = pictureView.bounds.height == 0 ? 0 : LRStatusCellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(offset)
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 添加ui
        setUI()
    }

    /// 计算高度
    func heightForRow(statusModel: LRStatusListModel)->CGFloat
    {
        self.statusModel = statusModel
        // 更新约束
        layoutIfNeeded()
        return CGRectGetMaxY(bottomView.frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     // MARK: - 添加UI
    private func setUI()
    {
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(bottomView)
        contentView.addSubview(pictureView)
        
        // 约束
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * LRStatusCellIconWidth + LRStatusCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(LRStatusCellMargin)
            make.left.equalTo(topView.snp_left).offset(LRStatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(LRStatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
            make.width.equalTo(290)
            make.height.equalTo(290)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(LRStatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            
            // 底部约束
//            make.bottom.equalTo(contentView.snp_bottom)
        }
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

     // MARK: - 控件懒加载
    /// 顶部视图
    private lazy var topView: LRTopView = LRTopView()
    /// 正文
    private lazy var contentLabel: UILabel = UILabel(title: "微博内容", color: UIColor.darkGrayColor(), fontSize: 15, layoutWidth: UIScreen.mainScreen().bounds.width - 2 * LRStatusCellMargin)
    /// 底部视图
    private lazy var bottomView: LRBottomView = LRBottomView()
    /// 图片视图
    private lazy var pictureView: LRPictureView = LRPictureView()
}
